package com.frame.auth.mvc.dao.pojo;

import com.alibaba.fastjson.JSON;
import com.frame.auth.constant.enums.AuthGrantTypeEnum;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;

import java.util.*;

public class CloudAuthClient implements ClientDetails {

    private Long id;

    private String appKey;

    private String appSecret;

    private String appName;

    private String appLogo;

    private String appDesc;

    private String callback;

    private String grantTypes;

    private String authScope;

    private String autoApprove;

    private Integer accessTokenValidity;

    private Integer refreshTokenValidity;

    private Date createTime;

    private Date updateTime;

    private Set<String> resourceIds;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getAppKey() {
        return appKey;
    }

    public void setAppKey(String appKey) {
        this.appKey = appKey;
    }

    public String getAppSecret() {
        return appSecret;
    }

    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public String getAppLogo() {
        return appLogo;
    }

    public void setAppLogo(String appLogo) {
        this.appLogo = appLogo;
    }

    public String getAppDesc() {
        return appDesc;
    }

    public void setAppDesc(String appDesc) {
        this.appDesc = appDesc;
    }

    public String getCallback() {
        return callback;
    }

    public void setCallback(String callback) {
        this.callback = callback;
    }

    public String getGrantTypes() {
        return grantTypes;
    }

    public void setGrantTypes(String grantTypes) {
        this.grantTypes = grantTypes;
    }

    public String getAuthScope() {
        return authScope;
    }

    public void setAuthScope(String authScope) {
        this.authScope = authScope;
    }

    public String getAutoApprove() {
        return autoApprove;
    }

    public void setAutoApprove(String autoApprove) {
        this.autoApprove = autoApprove;
    }

    public Integer getAccessTokenValidity() {
        return accessTokenValidity;
    }

    public void setAccessTokenValidity(Integer accessTokenValidity) {
        this.accessTokenValidity = accessTokenValidity;
    }

    public Integer getRefreshTokenValidity() {
        return refreshTokenValidity;
    }

    public void setRefreshTokenValidity(Integer refreshTokenValidity) {
        this.refreshTokenValidity = refreshTokenValidity;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    @Override
    public String getClientId() {
        return this.appKey;
    }

    @Override
    public Set<String> getResourceIds() {
        return this.resourceIds;
    }

    public void setResourceIds(Set<String> resourceIds) {
        this.resourceIds = resourceIds;
    }

    @Override
    public boolean isSecretRequired() {
        return false;
    }

    @Override
    public String getClientSecret() {
        return this.appSecret;
    }

    @Override
    public boolean isScoped() {
        return false;
    }

    @Override
    public Set<String> getScope() {
        Set<String> scopes = new HashSet<>();
        if (null != this.authScope && !"".equals(this.authScope)) {
            scopes.addAll(JSON.parseArray(this.authScope, String.class));
        }
        return scopes;
    }

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        List<Integer> types = null;
        if (null != this.grantTypes && !"".equals(this.grantTypes)) {
            types = JSON.parseArray(this.grantTypes, Integer.class);
        }
        return AuthGrantTypeEnum.getAuthGrantType(types);
    }

    @Override
    public Set<String> getRegisteredRedirectUri() {
        Set<String> urls = new HashSet<>();
        if (null != this.callback && !"".equals(this.callback)) {
            urls.add(this.callback);
        }
        return urls;
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return new ArrayList<>();
    }

    @Override
    public Integer getAccessTokenValiditySeconds() {
        return this.accessTokenValidity;
    }

    @Override
    public Integer getRefreshTokenValiditySeconds() {
        return this.refreshTokenValidity;
    }

    @Override
    public boolean isAutoApprove(String autoApprove) {
        if (null == autoApprove || "".equals(autoApprove)) {
            return false;
        }
        if ("true".equals(autoApprove)) {
            return true;
        }
        if ("false".equals(autoApprove)) {
            return false;
        }
        return null != this.authScope && !"".equals(this.authScope) && this.authScope.contains(autoApprove);
    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        return null;
    }
}